﻿using AutoStar.Common;
using AutoStar.ViewModel;
using SharpGL;
using SharpGL.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Windows.Forms.LinkLabel;

namespace AutoStar.Views.Pages
{
    /// <summary>
    /// PrintStatusView.xaml 的交互逻辑
    /// </summary>
    public partial class PrintStatusView : Page
    {
        public PrintStatusView()
        {
            InitializeComponent();
            DataContext = Global.printStatusVM;
            if (Global.printer!=null)
            {
                openGLControl1.OpenGLDraw += OpenGLDraw1_Loaded;
                openGLControl1.OpenGLInitialized += openGLControl1_OpenGLInitialized;
            }
        }
        //public static Printer printer = Printer.GetPrinter(150, 130, 100);//打印机
        public static Printer printer = Global.printer;//打印机
        int look = 300; 
        //double eyez = -80; 
        double eyez = 100; 
        private double rotation = 0;
        //List<Line3D> lines = new List<Line3D>();//用于在状态监测界面画路径的线
        List<Line3D> lines = Global.lines;//用于在状态监测界面画路径的线
        private bool isRight = false;
        private void OpenGLDraw1_Loaded(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
        {
            double NumericValue = Convert.ToDouble(txtZ.Text);
            Global.printStatusVM.NumericValue = NumericValue;
            double NumericValue2 = Convert.ToDouble(txtHeight.Text);
            bool IsAutoRotate = (bool)ToggleButton1.IsChecked;
            OpenGL gl = openGLControl1.OpenGL;
            if (NumericValue > 0)
            {
                printer.moveToAbsolute(printer.X, printer.Y, (float)NumericValue);
            }
            //gl.Viewport(0, 0, ((int)openGLControl1.ActualWidth), ((int)openGLControl1.ActualHeight));
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            //清除缓存操作，可以清除的缓存有：
            //GL_Color_Buffer_Bit颜色缓存、GL_Depth_Buffer_Bit深度缓存、
            //GL_Accum_Buffer_Bit累积缓存、GL_Stencil_Buffer_Bit模板缓存
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.LoadIdentity();
            gl.Perspective(50.0f, (double)openGLControl1.ActualWidth / (double)openGLControl1.ActualHeight, 0.01, 1000.0);
            //gl.LookAt(look, look, eyez, printer.maxX / 2, printer.maxY / 2, printer.maxZ / 2, 0, 0, -1);
            gl.LookAt(look, look, eyez, printer.maxX / 2, printer.maxY / 2, printer.maxZ / 2, 0, 0, 1);
            gl.MatrixMode(OpenGL.GL_MODELVIEW);
            gl.LoadIdentity();
            //加载单位矩阵，目的在于清除之前的矩阵操作(平移、缩放、旋转等操作)
            gl.Rotate(rotation, 0.0f, 0.0f, 1.0f);
            ////旋转矩阵操作，Rotate四个参数分别是：旋转角度（角度制单位）、X、Y、Z
            ////X、Y、Z表示：原点到（X，Y，Z）方向的矢量
            gl.Begin(OpenGL.GL_TRIANGLES);
            gl.Color(0.5f, 1.0f, 1.0f);
            gl.Vertex(printer.X + 0f, printer.Y + 0f, printer.Z + 0f);
            gl.Color(1.0f, 0.5f, 1.0f);
            gl.Vertex(printer.X - 10f, printer.Y + 10f, printer.Z + 20f);
            gl.Color(1.0f, 1.0f, 0.5f);
            gl.Vertex(printer.X - 10f, printer.Y - 10f, printer.Z + 20f);

            gl.Color(0.5f, 1.0f, 1.0f);
            gl.Vertex(printer.X + 0f, printer.Y + 0f, printer.Z + 0f);
            gl.Color(1.0f, 1.0f, 0.5f);
            gl.Vertex(printer.X - 10f, printer.Y - 10f, printer.Z + 20f);
            gl.Color(1.0f, 0.5f, 1.0f);
            gl.Vertex(printer.X + 10f, printer.Y - 10f, printer.Z + 20f);

            gl.Color(0.5f, 1.0f, 1.0f);
            gl.Vertex(printer.X + 0f, printer.Y + 0f, printer.Z + 0f);
            gl.Color(1.0f, 0.5f, 1.0f);
            gl.Vertex(printer.X + 10f, printer.Y - 10f, printer.Z + 20f);
            gl.Color(1.0f, 1.0f, 0.5f);
            gl.Vertex(printer.X + 10f, printer.Y + 10f, printer.Z + 20f);

            gl.Color(0.5f, 1.0f, 1.0f);
            gl.Vertex(printer.X + 0f, printer.Y + 0f, printer.Z + 0f);
            gl.Color(1.0f, 1.0f, 0.5f);
            gl.Vertex(printer.X - 10f, printer.Y + 10f, printer.Z + 20f);
            gl.Color(1.0f, 0.5f, 1.0f);
            gl.Vertex(printer.X + 10f, printer.Y + 10f, printer.Z + 20f);
            gl.End();

            //X、Y、Z轴线
            gl.Begin(OpenGL.GL_LINES);
            gl.Color(0.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 0.0f, 0.0f);
            gl.Vertex(400.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 400.0f, 0.0f);
            gl.Vertex(0.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 0.0f, 400.0f);
            gl.Color(1.0f, 0.0f, 0.0f);
            //gl.Begin(OpenGL.GL_TRIANGLES);
            //gl.Color(0.5f, 1.0f, 1.0f);
            //gl.Vertex(printer.X + 0f, printer.Y + 0f, printer.Z + 0f);
            //gl.Color(1.0f, 0.5f, 1.0f);
            //gl.Vertex(printer.X - 10f, printer.Y + 10f, printer.Z + 20f);
            //gl.Color(1.0f, 1.0f, 0.5f);
            //gl.Vertex(printer.X - 10f, printer.Y - 10f, printer.Z + 20f);

            //gl.Color(0.5f, 1.0f, 1.0f);
            //gl.Vertex(printer.X + 0f, printer.Y + 0f, printer.Z + 0f);
            //gl.Color(1.0f, 1.0f, 0.5f);
            //gl.Vertex(printer.X - 10f, printer.Y - 10f, printer.Z + 20f);
            //gl.Color(1.0f, 0.5f, 1.0f);
            //gl.Vertex(printer.X + 10f, printer.Y - 10f, printer.Z + 20f);

            //gl.Color(0.5f, 1.0f, 1.0f);
            //gl.Vertex(printer.X + 0f, printer.Y + 0f, printer.Z + 0f);
            //gl.Color(1.0f, 0.5f, 1.0f);
            //gl.Vertex(printer.X + 10f, printer.Y - 10f, printer.Z + 20f);
            //gl.Color(1.0f, 1.0f, 0.5f);
            //gl.Vertex(printer.X + 10f, printer.Y + 10f, printer.Z + 20f);

            //gl.Color(0.5f, 1.0f, 1.0f);
            //gl.Vertex(printer.X + 0f, printer.Y + 0f, printer.Z + 0f);
            //gl.Color(1.0f, 1.0f, 0.5f);
            //gl.Vertex(printer.X - 10f, printer.Y + 10f, printer.Z + 20f);
            //gl.Color(1.0f, 0.5f, 1.0f);
            //gl.Vertex(printer.X + 10f, printer.Y + 10f, printer.Z + 20f);

            ////gl.Color(0.5f, 0.5f, 0.5f);
            ////gl.Vertex(printer.maxX, printer.maxY, NumericValue2);
            ////gl.Vertex(printer.maxX, 0, NumericValue2);
            ////gl.Vertex(0, printer.maxY, NumericValue2);
            ////gl.Vertex(0, 0, NumericValue2);
            ////gl.Vertex(printer.maxX, 0, NumericValue2);
            ////gl.Vertex(0, printer.maxY, NumericValue2);

            //gl.End();

            ////路径
            //gl.Begin(OpenGL.GL_LINES);
            //gl.Color(0.0f, 0.0f, 0.0f);
            //gl.Vertex(0.0f, 0.0f, 0.0f);
            //gl.Vertex(400.0f, 0.0f, 0.0f);
            //gl.Vertex(0.0f, 0.0f, 0.0f);
            //gl.Vertex(0.0f, 400.0f, 0.0f);
            //gl.Vertex(0.0f, 0.0f, 0.0f);
            //gl.Vertex(0.0f, 0.0f, 400.0f);
            //gl.Color(1.0f, 0.0f, 0.0f);
            for (int i = 0; i < lines.Count; i++)
            {
                gl.Vertex(lines[i].start.X, lines[i].start.Y, lines[i].start.Z);
                gl.Vertex(lines[i].end.X, lines[i].end.Y, lines[i].end.Z);
            }
            gl.End();
            //OpenGL每绘制一次，rotation累加3.0f，gl.Rotate的旋转角就会增加3.0°，进而实现了图元绕Y轴旋转的效果
            if (IsAutoRotate)
            {
                if (rotation > 30) isRight = true;
                if (rotation < -30) isRight = false;
                rotation = isRight ? rotation - 0.5f : rotation + 0.5f;
            }
        }
        bool isMouseDown = false;
        Point downPoint = new Point(0, 0);
        Point movePoint = new Point(0, 0);
        Point upPoint = new Point(0, 0);
        private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
        {
            OpenGL gl = openGLControl1.OpenGL;
            gl.ClearColor(0.8f, 0.8f, 0.8f, 0.0f);
        }
        private void openGLControl1_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            downPoint = e.GetPosition(openGLControl1);
        }

        private void openGLControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                movePoint = e.GetPosition(openGLControl1);
                rotation -= (movePoint.X - downPoint.X) / 500.0;
                eyez -= (movePoint.Y - downPoint.Y) / 500.0;
            }
        }

        private void openGLControl1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
            upPoint = e.GetPosition(openGLControl1);
            rotation -= (movePoint.X - upPoint.X) / 500.0;
            eyez -= (movePoint.Y - upPoint.Y) / 500.0;
        }

        private void txtZ_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (double.Parse(txtZ.Text)>100)
                txtZ.Text = "100";
            string newText = txtZ.Text;
            if (!double.TryParse(newText, out _))
            {
                // 如果输入值不是有效的double类型数据，则将文本重置为上一次有效的数值
                txtZ.Text = GetLastValidDoubleValue(newText);
            }
        }
        private void txtHeight_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (double.Parse(txtHeight.Text) > 100)
                txtHeight.Text = "100";
            string newText = txtHeight.Text;
            if (!double.TryParse(newText, out _))
            {
                // 如果输入值不是有效的double类型数据，则将文本重置为上一次有效的数值
                txtHeight.Text = GetLastValidDoubleValue(newText);
            }
        }
        private string GetLastValidDoubleValue(string currentText)
        {
            double lastValidValue;
            if (double.TryParse(currentText, out lastValidValue))
            {
                return lastValidValue.ToString();
            }
            else
            {
                return "0.00"; // 如果无法解析出有效的double类型数据，则返回默认值
            }
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.FontSize = e.NewSize.Height / 23;

        }
    }
}
